<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => {
        unfoldFlag = v;
      }
        ">
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData :tableThead="tableThead" :dataList="dataList" :loading="loading">
        <template v-slot:fbillflag="{ row }">
          <span>{{ queryfbillflagList(row.fbillflag) }}</span>
        </template>
        <template v-slot:seeDetail="{ row }">
          <span class="successText" @click="lookView(row)">查看详情</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="handleCurrentChange" :page="page" :size="size" :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <el-dialog title="详情" :visible.sync="dialogVisibleLookView" width="80%" :before-close="() => {
        dialogVisibleLookView = false;
      }
        ">
        <el-row :gutter="15">
          <el-col v-show="!item.slotName" v-for="(item, index) in tableThead" :key="index" :span="4"
            style="margin: 10px 0px">
            <span>{{ item.label }}：</span>
            <span>{{ lookViewObj[item.prop] }}</span>
          </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column v-for="(item, index) in dialogtableThead" :key="index" :prop="item.prop" :label="item.label"
            min-width="100" align="center">
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleLookView = false">关 闭</el-button>
        </span>
      </el-dialog>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
//NC批次
import ncproductionApi from "@/api/production-management-nc/index";
import { getOneYearAgo, getCurrentDate } from "@/utils/time";
import ncBasicinfoApi from "@/api/nc-basic-info/index";
import { fbillflagList } from "@/utils/NcQueryData";
export default {
  components: {
    MainSlot,
    Pagination,
    SearchForm,
    TableData,
  },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      page: 1,
      size:30,
      total: 10,
      searchFormData: {
        startDate: getOneYearAgo(),
        endDate: getCurrentDate(),
      },
      lookViewObj: {},
      dataList: [],
      dialogVisibleLookView: false,
      tableData: [],
      OrgDeptList: [],
      dialogtableThead: [
        {
          label: "物料编码",
          prop: "cmaterialvcode",
        },
        {
          label: "物料名称",
          prop: "cmaterialvname",
        },
        {
          label: "主单位",
          prop: "cunitname",
        },
        {
          label: "辅单位",
          prop: "castunitname",
        },
        {
          label: "换算率",
          prop: "vchangerate",
        },
        {
          label: "批次号",
          prop: "vbatchcode",
        },
        {
          label: "养殖批次号",
          prop: "vproductbatch",
        },
        {
          label: "应发辅数量",
          prop: "nshouldassistnum",
        },
        {
          label: "应发主数量",
          prop: "nshouldnum",
        },

        {
          label: "实发辅数量",
          prop: "nassistnum",
        },

        {
          label: "实发主数量",
          prop: "nnum",
        },
        {
          label: "活鱼品规",
          prop: "vfree1name",
        },
        {
          label: "条数",
          prop: "vbdef1",
        },
        {
          label: "均重",
          prop: "vbdef2",
        },
      ],
      tableThead: [
        {
          isShow: true,
          label: "单据号",
          prop: "vbillcode",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "单据日期",
          prop: "dbilldate",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "仓库",
          prop: "cwarehousename",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "业务员",
          prop: "username",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "生产部门",
          prop: "cdptname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "查看详情",
          slotName: "seeDetail",
          minWidth: "120px",
        },
      ],

      formItemList: [
        {
          label: "单据号",
          type: "input",
          param: "vbillcode",
          multiple: false,
          isShow: true,
        },
        {
          label: "批次号",
          type: "input",
          param: "vbatchcode",
          multiple: false,
          isShow: true,
        },
        // {
        //   label: "仓库",
        //   type: "select",
        //   selectOptions: [],
        //   param: "cwarehouseid",
        //   defaultSelect: "", // 下拉框默认选中项
        //   multiple: false,
        //   isShow: true,
        // },
        {
          label: "鱼塘名称",
          type: "select",
          selectOptions: [],
          param: "cdptid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "业务员",
          type: "input",
          param: "username",
          multiple: false,
          isShow: true,
          // label: "业务员",
          // type: "select",
          // selectOptions: [],
          // param: "username",
          // defaultSelect: "", // 下拉框默认选中项
          // multiple: false,
          // isShow: true,
        },
        {
          label: "日期查询",
          type: "datetimerange",
          param: "dateTime",
          defaultSelect: [getOneYearAgo(), getCurrentDate()],
          multiple: false,
          isShow: true,
        },
      ],
      PsndocList: [],
      StordocList: [],
    };
  },
  mounted() {
    this.queryPageByCondition();
    this.queryStordoc(); //Stordoc仓库
    this.queryPsndoc(); //业务员
    this.queryOrgDept();
    // this.$store.dispatch("NcBasicInfo/queryOrgDept");
  },
  computed: {
    OrgDept() {
      return this.$store.state.NcBasicInfo.OrgDept || [];
    },
  },

  methods: {
    // 查询仓库鱼塘
    queryStordoc() {
      ncBasicinfoApi
        .queryStordoc({ pk_org: JSON.parse(localStorage.ncOrg) })
        .then((res) => {
          this.StordocList = res.data || [];
          this.setFormOptions();
        });
    },
    // 查询部门
    queryPsndoc() {
      ncBasicinfoApi
        .queryPsndoc({ pk_org: JSON.parse(localStorage.ncOrg) })
        .then((res) => {
          this.PsndocList = res.data || [];
          this.setFormOptions();
        });
    },
    // 查询部门
    queryOrgDept() {
      ncBasicinfoApi
        .queryOrgDept({ pk_org: JSON.parse(localStorage.ncOrg) })
        .then((res) => {
          this.OrgDeptList = res.data || [];
          this.setFormOptions();
        });
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = [];
      list.forEach((item, index) => {
        // if (item.param.indexOf("cwarehouseid") !== -1) {
        //   let selectOptions = this.StordocList.map((data) => {
        //     return {
        //       text: data.name,
        //       value: data.pk_stordoc,
        //     };
        //   });
        //   item.selectOptions = selectOptions;
        // }
        if (item.param.indexOf("cdptid") !== -1) {
          let selectOptions = this.OrgDeptList.map((data) => {
            return {
              text: data.name,
              value: data.pk_dept,
            };
          });
          item.selectOptions = selectOptions;
        }
        if (item.param.indexOf("cmaterialvid") !== -1) {
          let selectOptions = this.MaterialList.map((data) => {
            return {
              text: data.name,
              value: data.pk_material,
            };
          });
          item.selectOptions = selectOptions;
        }
        if (item.param.indexOf("username") !== -1) {
          let selectOptions = this.PsndocList.map((data) => {
            return {
              text: data.name,
              value: data.name,
            };
          });
          item.selectOptions = selectOptions;
        }
      });
      this.$nextTick(() => {
        this.$set(this, "formItemList", list);
      });
    },

    queryfbillflagList(e) {
      return fbillflagList(e);
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
        },
      };
      this.loading = true;
      ncproductionApi
        .queryFinprodin(params)
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.message || "分页查询失败");
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      if (data.dateTime && data.dateTime.length) {
        data.startDate = data.dateTime[0];
        data.endDate = data.dateTime[1];
      }
      delete data.dateTime;
      this.searchFormData = { ...data };

      for (let key in this.searchFormData) {
        if (!this.searchFormData[key]) {
          this.searchFormData[key] = null;
        }
      }
      console.log(this.searchFormData, data, "this.searchFormData");
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    //查看详情
    lookView(row) {
      let obj = JSON.parse(JSON.stringify(row));
      this.lookViewObj = obj;
      ncproductionApi.queryFinprodinDtl({
        orderId: obj.cgeneralhid,
      }).then(res => {
        this.tableData = res.data || [];
        this.dialogVisibleLookView = true;
      }).catch(err => {
        this.tableData = [];
        this.dialogVisibleLookView = true;
      });

    },
  },
};
</script>
<style lang="scss" scoped></style>